﻿(function (wtm) {
    wtm.combobox_Render = function (para) {
        if (para.beforeMount.length > 0) {
            eval(para.beforeMount + '(para);');
        }

        //预处理url
        para.url = wtm.getUrl(para.url);
        para.triggerUrl = wtm.getUrl(para.triggerUrl);

        var $el = $('#' + para.id);
        var $div = $('#div' + para.id);

        if ($el.attr('lay-verify') != undefined) {
            $div.attr('lay-verify', true);
        }

        var option = {
            el: '#div' + para.id,
            delay: para.delay,
            name: null,
            disabled: para.disabled,
            filterable: true,
            initValue: para.initValue,
            tips: para.emptyText,
            radio: !para.multi,
            clickClose: !para.multi,
            prop: {
                name: para.textKey,
                value: para.valueKey,
                selected: 'Selected'
            },
            theme: {
                color: '#1E9FFF'
            },
            on: function (data) {
                $div.find('input[wtm-field]').remove();
                $el.val('');

                for (var i = 0; i < data.arr.length; i++) {
                    $div.append(`<input wtm-field hidden name='${para.fieldName}' value='${data.arr[i][para.valueKey]}'/>`);
                    $el.val('hasValue');
                }

                if (data.isAdd && !para.multi) {
                    data.triggerUrl = para.triggerUrl + '?id=' + data.change[0][para.valueKey];
                    //处理用户事件
                    if (para.changeFunc.length > 0) {
                        eval(para.changeFunc + '(data);');
                    }

                    wtm.ChainChange(data.triggerUrl, $el);
                }
                else {
                    //处理用户事件
                    if (para.changeFunc.length > 0) {
                        eval(para.changeFunc + '(data);');
                    }
                }
            }
        };


        //初始化数据 及初值
        if (para.url.length > 0) {
            $.get(para.url, function (resp) {
                option.data = resp.Data;
                xmSelect.render(option);

                if (para.mounted.length > 0) {
                    eval(para.mounted + '(para);');
                }
            });
        }
        else {
            option.data = para.data;
            xmSelect.render(option);

            if (para.mounted.length > 0) {
                eval(para.mounted + '(para);');
            }
        }

        for (var i = 0; i < para.initValue.length; i++) {
            $div.append(`<input wtm-field hidden name='${para.fieldName}' value='${para.initValue[i]}'/>`);
            $el.val('hasValue');
        }

    };


    wtm.ChainChange = function (url, $self) {
        var linkto = $self.attr('wtm-linkto');

        if (linkto == undefined) {
            return;
        }
        var $form = $self.closest('form');

        var $target = $form.find('[wtm-name="' + linkto + '"]');

        if ($target.length == 0) {
            return;
        }

        var controltype = $target.attr('wtm-ctype');

        var targetid = $target.attr('id');

        //clear
        switch (controltype) {
            case 'combobox':
                xmSelect.get('#div' + targetid, true).update({ url: null, data: [] });
                break;
            case 'checkbox':
                $target.html('');
                form.render('checkbox', targetfilter);
                break;
            case 'radio':
                $target.html('');
                form.render('radio', targetfilter);
                break;
            case 'tree':
                layui.tree.reload(targetid, {
                    data: []
                });
                break;
            case 'transfer':
                layui.transfer.reload(targetid, {
                    data: []
                });
            default:
        }
        if (url != '') {
            $.get(url, {}, function (data, status) {
                if (status == 'success') {
                    var i = 0;
                    var item = null;

                    if (controltype == 'tree') {
                        layui.tree.reload(targetid, {
                            data: ff.getTreeItems(data.Data)
                        });
                    }
                    if (controltype == 'transfer') {
                        layui.transfer.reload(targetid, {
                            data: ff.getTransferItems(data.Data)
                        });
                    }

                    if (controltype == 'combobox') {
                        xmSelect.get('#div' + targetid, true).update({ data: data.Data });
                    }
                    if (controltype == 'checkbox') {
                        for (i = 0; i < data.Data.length; i++) {
                            item = data.Data[i];
                            if (item.Selected === true) {
                                target.append(`<input type='checkbox' name='${targetname}' value='${item.Value}' title='${item.Text}' checked/>`);
                            }
                            else {
                                target.append(`<input type='checkbox' name='${targetname}' value='${item.Value}' title= '${item.Text}'/>"`);
                            }
                        }
                        form.render('checkbox', targetfilter);
                    }
                    if (controltype == 'radio') {
                        for (i = 0; i < data.Data.length; i++) {
                            item = data.Data[i];
                            if (item.Selected === true) {
                                target.append(`<input type='radio' name='${targetname}' value='${item.Value}' title='${item.Text}' checked/>`);
                            }
                            else {
                                target.append(`<input type='radio' name='${targetname}' value='${item.Value}' title='${item.Text}'/>`);
                            }
                        }
                        form.render('radio', targetfilter);
                    }

                }
                else {
                    layer.alert(ff.DONOTUSE_Text_FailedLoadData);
                }
            });
        }

        wtm.ChainChange('', $target);
    }

})(wtm);